package top.abilities.util.util;

import org.apache.commons.lang.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @Auhtor: ciro
 * @Description
 * @Date: 2018/1/8 15:38
 * @Modified By:
 */
public class LocalDateUtil {

    /**
     * 获取day的起始时间点
     * @param localDateTime
     * @return
     */
    public static String getDateStart(LocalDateTime localDateTime){
        /*localDateTime=LocalDateTime.of(localDateTime.getYear(),localDateTime.getMonth(),localDateTime.getDayOfMonth(),0,0,0);
        return  localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));*/
        return localDateTime.with(LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    public static Date getDateStartToDate(LocalDateTime localDateTime){
        /*localDateTime=LocalDateTime.of(localDateTime.getYear(),localDateTime.getMonth(),localDateTime.getDayOfMonth(),0,0,0);
        return  localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));*/
        return LocalDateUtil.strToDate(localDateTime.with(LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
    /**
     * 获取day的起始时间点
     * @param date
     * @return
     */
    public static String getDateStart(Date date){
        return getDateStart(dateToLocalDateTime(date));
    }
    /**
     * 获取day的起始时间点
     * @param date
     * @return
     */
    public static Date getDateStartToDate(Date date){
        return getDateStartToDate(dateToLocalDateTime(date));
    }
    /**
     * 获取day的起始时间点
     * @param dateStr
     * @return
     */
    public static String getDateStart(String dateStr){
        if(StringUtils.isEmpty(dateStr)){
            return null;
        }
        return getDateStart(strToDate(dateStr));
    }
    /**
     * 获取day的结束时间点
     * @param localDateTime
     * @return
     */
    public static String getDateEnd(LocalDateTime localDateTime){
        /*localDateTime=LocalDateTime.of(localDateTime.getYear(),localDateTime.getMonth(),localDateTime.getDayOfMonth(),23,59,59);
        return  localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));*/
        return localDateTime.with(LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    /**
     * 获取day的结束时间点
     * @param date
     * @return
     */
    public static String getDateEnd(Date date){
        return getDateEnd(dateToLocalDateTime(date));
    }
    public static String getDateEnd(String dateStr){
        if(StringUtils.isEmpty(dateStr)){
            return null;
        }
        return getDateEnd(strToDate(dateStr,"yyyy-MM-dd HH:mm:ss"));
    }
    public static String getDateEnd(String dateStr,String format){
        if(StringUtils.isEmpty(dateStr)){
            return null;
        }
        return getDateEnd(strToDate(dateStr,format));
    }
    public static Date getDateEndToDate(Date date){
        return LocalDateUtil.strToDate(getDateEnd(dateToLocalDateTime(date)));
    }
    public static Date getDateEndToDate(String date,String format){
        return LocalDateUtil.strToDate(getDateEnd(date,format));
    }

    /**
     * 获取当前月第一天的起始时间
     * @param localDateTime
     * @return
     */
    public static String getFirstDayOfMonth(LocalDateTime localDateTime){
        LocalDateTime firstDateTime=localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
        return firstDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    /**
     * 获取当前月第一天的起始时间
     * @param date
     * @return
     */
    public static String getFirstDayOfMonth(Date date){
        return getFirstDayOfMonth(dateToLocalDateTime(date));
    }
    /**
     * 获取当前月第一天的起始时间
     * @param dateStr
     * @return
     */
    public static String getFirstDayOfMonth(String dateStr){
        return getFirstDayOfMonth(strToDate(dateStr));
    }

    /**
     * 获取当前月最后一天的结束时间
     * @param localDateTime
     * @return
     */
    public static String getLastDayOfMonth(LocalDateTime localDateTime){
        return localDateTime.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    /**
     * 获取当前月最后一天的结束时间
     * @param date
     * @return
     */
    public static String getLastDayOfMonth(Date date){
        return getLastDayOfMonth(dateToLocalDateTime(date));
    }
    /**
     * 获取当前月最后一天的结束时间
     * @param dateStr
     * @return
     */
    public static String getLastDayOfMonth(String dateStr){
        return getLastDayOfMonth(strToDate(dateStr));
    }


    /**
     * 获取当前周第一天起始时间
     * @param localDateTime
     * @return
     */
    public static String getFirstDayOfWeek(LocalDateTime localDateTime){
        return localDateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).with(LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    /**
     * 获取当前周第一天起始时间
     * @param date
     * @return
     */
    public static String getFirstDayOfWeek(Date date){
        return getFirstDayOfWeek(dateToLocalDateTime(date));
    }
    /**
     * 获取当前周第一天起始时间
     * @param dateStr
     * @return
     */
    public static String getFirstDayOfWeek(String dateStr){
        return getFirstDayOfWeek(strToDate(dateStr));
    }

    /**
     * 获取当前周最后一天结束时间
     * @param localDateTime
     * @return
     */
    public static String getLastDayOfWeek(LocalDateTime localDateTime){
        return localDateTime.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).with(LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    /**
     * 获取当前周最后一天结束时间
     * @param date
     * @return
     */
    public static String getLastDayOfWeek(Date date){
        return getLastDayOfWeek(dateToLocalDateTime(date));
    }
    /**
     * 获取当前周最后一天结束时间
     * @param dateStr
     * @return
     */
    public static String getLastDayOfWeek(String dateStr){
        return getLastDayOfWeek(strToDate(dateStr));
    }

    /**
     *  获取当前年第一天的起始时间
     * @param localDateTime
     * @return
     */
    public static String getFirstDayOfYear(LocalDateTime localDateTime){
        return localDateTime.with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    /**
     * 获取当前年第一天的起始时间
     * @param date
     * @return
     */
    public static String getFirstDayOfYear(Date date){
        return getFirstDayOfYear(dateToLocalDateTime(date));
    }
    /**
     * 获取当前年第一天的起始时间
     * @param dateStr
     * @return
     */
    public static String getFirstDayOfYear(String dateStr){
        return getFirstDayOfYear(strToDate(dateStr));
    }

    /**
     *  获取当前年最后一天的起始时间
     * @param localDateTime
     * @return
     */
    public static String getLastDayOfYear(LocalDateTime localDateTime){
        return localDateTime.with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
    /**
     * 获取当前年最后一天的起始时间
     * @param date
     * @return
     */
    public static String getLastDayOfYear(Date date){
        return getLastDayOfYear(dateToLocalDateTime(date));
    }
    /**
     * 获取当前年最后一天的起始时间
     * @param dateStr
     * @return
     */
    public static String getLastDayOfYear(String dateStr){
        return getLastDayOfYear(strToDate(dateStr));
    }
    /**
     * Date 转 LocalDateTime
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
    }

    /**
     * Date 转 String
     * @return
     */
    public static String  dateToString(Date date,String pattern){
        LocalDateTime localDateTime=LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * long类型时间转换成String类型
     * @param l
     * @param format
     * @return
     */
    public static String dateToString(Long l,String format){
        LocalDateTime localDateTime=LocalDateTime.ofInstant(Instant.ofEpochMilli(l),ZoneId.systemDefault());
        localDateTime.format(DateTimeFormatter.ofPattern(format));
        return localDateTime.format(DateTimeFormatter.ofPattern(format));
    }
    /**
     * timestamp转换成String类型
     * @param timeStamp
     * @param format
     * @return
     */
    public static String dateToString(Timestamp timeStamp, String format){
        LocalDateTime localDateTime=LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStamp.getTime()),ZoneId.systemDefault());
        return localDateTime.format(DateTimeFormatter.ofPattern(format));
    }
    /**
     * String 转 Date 类型
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate){
        if(StringUtils.isEmpty(strDate)){
            return null;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date= null;
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public static Date strToDate(String strDate,String format){
        if(StringUtils.isEmpty(strDate)){
            return null;
        }
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        Date date= null;
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * Timestamp 转 LocalDateTime
     * @param dateTime
     * @return
     */
    public Timestamp localToTimestamp(LocalDateTime dateTime){
        return Timestamp.valueOf(dateTime);
    }

    /**
     * LocalDateTime 转 Timestamp
     * @param dateTime
     * @return
     */
    public LocalDateTime timestampToLocal(Timestamp dateTime){
        return dateTime.toLocalDateTime();
    }

    /**
     * Date 转 LocalDateTime
     * @param date
     * @return
     */
    public LocalDateTime dateToLocal(Date date){
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime;
    }
    /**
     * LocalDateTime  转 Date
     * @param localDateTime
     * @return
     */
    public Date localTodate(LocalDateTime localDateTime){
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }
    public static void main(String[] args) {
        /*System.out.println("当天开始时间："+getDateStart("2016-12-12 08:09:23"));
        System.out.println("当天结束时间："+getDateEnd(LocalDateTime.now()));
        System.out.println("当前周的第一天："+getFirstDayOfWeek("2016-12-14 08:09:23"));
        System.out.println("当前周的最后一天："+getLastDayOfWeek("2016-12-14 08:09:23"));

        System.out.println("当前年的第一天："+getFirstDayOfYear("2016-12-14 08:09:23"));
        System.out.println("当前年的最后一天："+getLastDayOfYear("2016-12-14 08:09:23"));*/
       // System.out.println(getFirstDayOfWeek(LocalDateTime.now()));
        System.out.println(LocalDateUtil.dateToString(new Date(),"yyyyMMddHHmmss"));
        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
        System.out.println(LocalDateUtil.getDateEndToDate("2099-12-31","yyyy-MM-dd"));
    }
}
